package com.gst.handfood.base;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gst.handfood.controller.LoadingPage;
import com.gst.handfood.utils.UIUtils;
import com.loopj.android.http.RequestParams;

import butterknife.ButterKnife;

/**
 * Created by YBH on 2017/10/31.
 */

public abstract class BaseFragment extends Fragment {
    private LoadingPage loadingPage;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        loadingPage = new LoadingPage(container.getContext()) {
            @Override
            public int LayoutId() {
                return getLayoutId();
            }

            @Override
            protected void onSuccess(ResultState resultState, View successView) {
                ButterKnife.bind(BaseFragment.this,successView);
                //初始化标题
                initTiTle();
                initData(resultState.getContent());
            }

            @Override
            protected String url() {
                return getUrl();
            }

            @Override
            protected RequestParams params() {
                return getParams();
            }
        };
        return loadingPage;
    }

    protected abstract void initData(String content);

    protected abstract void initTiTle();

    protected abstract RequestParams getParams();

    protected abstract String getUrl();

    public abstract int getLayoutId();

    /**
     * 当页面穿件完成是就开始显示布局
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        UIUtils.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                show();
            }
        }, 800);

    }

    private void show() {
        loadingPage.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
